﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Text;
using MySql.Data.MySqlClient;
using System.Data;
using System.ComponentModel;

namespace AdministrationWeb.Administration
{
    public partial class EditCourses : System.Web.UI.Page
    {

        private const string CONNECTION_STRING_NAME = "ApplicationConnectionString";
        
        protected void Page_Load(object sender, EventArgs e)
        {
            lblMessage.Text = "";
            txtCourseSearch.Focus();
        }

        protected void btnInsert_Click(object sender, EventArgs e)
        {
            try
            {             

                int numRows = SqlCourses.Insert();

                if (numRows > 0)
                {
                    txtNewName.Text = "";
                    gdvCourses.DataBind();
                    lblMessage.Text = "Course succesfuly created";
                }
                else
                {
                    lblMessage.Text = "Unable to create Course, please try again";
                }

            }catch (Exception ex)
            {
                lblMessage.Text = "Error creating Course: " + ex.Message;
            }

        }

        protected void gdvCourses_RowUpdated(object sender, GridViewUpdatedEventArgs e)
        {
            //test for and display error or succes
            if (e.Exception != null)
            {
                lblMessage.Text = "Error updating pathway: " + e.Exception.Message;
                e.ExceptionHandled = true;
                e.KeepInEditMode = true;
            }
            else if (e.AffectedRows == 0)
            {
                lblMessage.Text = "Unknown error, course not updated, try again";
            }
            else
            {
                lblMessage.Text = "Course updated succesfuly";
            }
        }

        protected void gdvCourses_RowDeleted(object sender, GridViewDeletedEventArgs e)
        {
            //test for and display error or succes
            if (e.Exception != null)
            {
                lblMessage.Text = "Error Deleting pathway: " + e.Exception.Message;
                e.ExceptionHandled = true;
            }
            else if (e.AffectedRows == 0)
            {
                lblMessage.Text = "Unknown error, course not deleted, try again";
            }
            else
            {
                lblMessage.Text = "Course deleted succesfuly";
            }
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            gdvCourses.DataBind();
        }
    }
}